import 'package:flutter/material.dart';
import 'package:ichinesereader/models/report/studentLevelProgress.dart';
import 'package:intl/intl.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:get/get.dart';

class LevelProgressChart extends StatefulWidget {
  const LevelProgressChart({
    Key? key,
    required this.levelchartdata,
  }) : super(key: key);
  final List<StudentLevelProgress> levelchartdata;
  @override
  _LevelProgressChart createState() => _LevelProgressChart();
}

class _LevelProgressChart extends State<LevelProgressChart> {
  List<StudentLevelProgress> newLevel = [];
  @override
  void initState() {
    if (widget.levelchartdata.isNotEmpty) {
      StudentLevelProgress last = new StudentLevelProgress();
      last.startDate = DateTime.now().millisecondsSinceEpoch;
      last.endDate = DateTime.now().millisecondsSinceEpoch;
      last.level =
          widget.levelchartdata[widget.levelchartdata.length - 1].level;
      newLevel.addAll(widget.levelchartdata);
      newLevel.add(last);
    }

    super.initState();
  }

  SfCartesianChart _buildDefaultStepLineChart() {
    return SfCartesianChart(
      plotAreaBorderWidth: 0,
      title: ChartTitle(text: 'Level Progress'.tr),
      primaryXAxis: CategoryAxis(
          majorGridLines: const MajorGridLines(width: 0), interval: 1),
      primaryYAxis: NumericAxis(
          axisLine: const AxisLine(width: 0),
          minimum: 0,
          interval: 1,
          majorTickLines: const MajorTickLines(size: 0),
          title: AxisTitle(text: 'Level'),
          labelFormat: 'Level {value}'),
      tooltipBehavior: TooltipBehavior(enable: true),
      series: <StepLineSeries<StudentLevelProgress, String>>[
        StepLineSeries<StudentLevelProgress, String>(
          dataSource: newLevel,
          xValueMapper: (StudentLevelProgress sales, _) =>
              DateFormat('MM/dd/yyyy')
                  .format(DateTime.fromMillisecondsSinceEpoch(sales.startDate!))
                  .toString(),
          yValueMapper: (StudentLevelProgress sales, _) =>
              int.parse(sales.level!.replaceAll('Level ', "")),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildDefaultStepLineChart();
  }
}
